package com.logicbus.backend;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 服务路径
 *
 * @since 1.6.14.12 [20210617 duanyy]
 */
public interface ServicePath {
    public final static String Delimiter = "/";
    public String getId();
    public String getPackage();
    public void setPath(String path);
    public ServicePath append(String id,boolean copy);
    public ServicePath append(ServicePath path,boolean copy);
    public boolean isRoot();
    public ServicePath subPath(int start,int end);
    public List<String> list();

    public static class Default implements ServicePath{
        protected List<String> content = new ArrayList<String>();

        public Default(String path){
            parsePath(path);
        }

        @Override
        public void setPath(String path){
            content.clear();
            parsePath(path);
        }

        protected void parsePath(String path) {
            StringTokenizer t = new StringTokenizer(path,Delimiter);
            while (t.hasMoreTokens()){
                String value = t.nextToken();
                if (StringUtils.isNotEmpty(value)){
                    content.add(value);
                }
            }
        }

        public Default(List<String> list){
            content = list;
        }

        public String format(int start,int end){
            StringBuffer buf = new StringBuffer();
            boolean isEmpty = true;
            for (int i = start ; i < end ; i ++){
                if (i < 0 || i >= content.size()){
                    break;
                }
                isEmpty = false;
                buf.append(Delimiter).append(content.get(i));
            }
            return isEmpty?Delimiter:buf.toString();
        }

        public Default(List<String> list,int start,int end){
           for (int i = start ; i < end ; i ++){
                if (i < 0 || i >= list.size()){
                    break;
                }
                content.add(list.get(i));
           }
        }

        @Override
        public String getId() {
            if (content.size() < 1){
                return null;
            }
            return content.get(content.size() - 1);
        }

        @Override
        public String getPackage() {
            return format(0,content.size()-1);
        }

        @Override
        public ServicePath append(String id,boolean copy) {
            if (copy){
                ServicePath newPath = new Default(content,0,content.size());
                return newPath.append(id,false);
            }else{
                if (StringUtils.isNotEmpty(id)){
                    content.add(id);
                }
                return this;
            }
        }

        @Override
        public ServicePath append(ServicePath path,boolean copy) {
            if (copy){
                ServicePath newPath = new Default(content,0,content.size());
                return newPath.append(path,false);
            }else{
                List<String> list = path.list();
                for (String item:list){
                    content.add(item);
                }
                return this;
            }
        }

        @Override
        public boolean isRoot() {
            return content.size() <= 0;
        }

        @Override
        public ServicePath subPath(int start, int end) {
            ServicePath newPath = new Default(content,start,end);
            return newPath;
        }

        @Override
        public List<String> list() {
            return content;
        }
    }
}
